
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.base.wf.AbstractWorkflow;
import com.wyzz.hr.bo.IEmailsendBO;
import com.wyzz.hr.bo.IEmailtemplateBO;
import com.wyzz.hr.bo.IEmpApproverBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.pojo.Emailtemplate;
import com.wyzz.hr.spring.SpringBeanFactory;

import java.lang.reflect.Method;
import java.util.*;


public class EmpApproverBoImpl implements IEmpApproverBo {

	public EmpApproverBoImpl() {
	}

	public synchronized Map saveOrUpdateFlowData(Object objList[]) {
		Map templateMap = getTemplateMap();
		AbstractWorkflow wf = null;
		Map msgMap = new HashMap();
		String operate = null;
		int maxId = 0;
		Object arr$[] = objList;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Object obj = arr$[i$];
			wf = getWorkflow(obj);
			operate = wf.getOperate();
			int dbMaxId = getMaxId(obj, new Integer[0]).intValue();
			if (dbMaxId == maxId)
				maxId++;
			else
				maxId = dbMaxId;
			if (operate.contains("create"))
				wf.setDocNo(Integer.valueOf(maxId));
			if (!wf.setWorkflowFields(wf, templateMap, msgMap))
				return msgMap;
			wf.msgMapAdd(wf, msgMap);
		}

		List dataList = Arrays.asList(objList);
		sl_BO.addLog(dataList);
		es_BO.saveAndSendMail(dataList);
		sl_BO.saveOrupdate(dataList);
		return msgMap;
	}

	private AbstractWorkflow getWorkflow(Object obj) {
		String methodName = "getWorkflow";
		Class objClass = obj.getClass();
		try {
			Method method = objClass.getMethod(methodName, (Class[]) null);
			return (AbstractWorkflow) method.invoke(obj, (Object[]) null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public synchronized Integer getMaxId(Object obj, Integer currentNo[]) {
		if (currentNo.length > 0 && currentNo[0] != null)
			return Integer.valueOf(currentNo[0].intValue() + 1);
		List result = AbstractWorkflow.getPropertyName(obj);
		List maxIDList = sl_BO.exeHqlList((new StringBuilder()).append(
				"select max(").append((String) result.get(1)).append(") from ")
				.append((String) result.get(0)).toString());
		Integer maxID;
		if (maxIDList == null || maxIDList.size() < 1
				|| maxIDList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxIDList.get(0))
					.intValue());
		return maxID;
	}

	private Map getTemplateMap() {
		IEmailtemplateBO templateBo = (IEmailtemplateBO) SpringBeanFactory
				.getBean("emailtemplateBO");
		Map rs = new HashMap();
		List templateList = templateBo.getList();
		Emailtemplate tmp;
		for (Iterator i$ = templateList.iterator(); i$.hasNext(); rs.put(tmp
				.getEtTitleNo(), tmp))
			tmp = (Emailtemplate) i$.next();

		return rs;
	}

	public void setSl_BO(ISysLogBO sl_BO) {
		this.sl_BO = sl_BO;
	}

	public ISysLogBO getSl_BO() {
		return sl_BO;
	}

	public IEmailsendBO getEs_BO() {
		return es_BO;
	}

	public void setEs_BO(IEmailsendBO es_BO) {
		this.es_BO = es_BO;
	}

	protected ISysLogBO sl_BO;
	protected IEmailsendBO es_BO;
}
